# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=opencv

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.4.9
pkgrel=2
pkgdesc="Open Source Computer Vision Library (mingw-w64)"
arch=('any')
url="http://opencv.org/"
depends=(#"${MINGW_PACKAGE_PREFIX}-gst-plugins-base0.10"
        "${MINGW_PACKAGE_PREFIX}-libjpeg"
        "${MINGW_PACKAGE_PREFIX}-libpng"
        "${MINGW_PACKAGE_PREFIX}-libtiff"
        "${MINGW_PACKAGE_PREFIX}-jasper"
        "${MINGW_PACKAGE_PREFIX}-ffmpeg"
        "${MINGW_PACKAGE_PREFIX}-openexr"
        "${MINGW_PACKAGE_PREFIX}-intel-tbb"
        "${MINGW_PACKAGE_PREFIX}-zlib"
        #"${MINGW_PACKAGE_PREFIX}-qt5"
        #"${MINGW_PACKAGE_PREFIX}-gtkglext"
        #"${MINGW_PACKAGE_PREFIX}-gtk2"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
            "${MINGW_PACKAGE_PREFIX}-qt5"
            "${MINGW_PACKAGE_PREFIX}-python2-numpy"
            "${MINGW_PACKAGE_PREFIX}-python2-sphinx"
            "${MINGW_PACKAGE_PREFIX}-eigen3"
            )
optdepends=("${MINGW_PACKAGE_PREFIX}-eigen3"
            #'libcl: For coding with OpenCL'
            "${MINGW_PACKAGE_PREFIX}-python2-numpy: Python 2.x interface")
source=(#"http://downloads.sourceforge.net/opencvlibrary/${_realname}-$pkgver.zip"
        https://github.com/Itseez/opencv/archive/$pkgver.tar.gz
        'mingw-w64-cmake.patch')
md5sums=('cc0a8307403ff471f554197401ec0eb9'
         'b92aa803cbf6de35851a86bf69d4eeb3')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "$srcdir/mingw-w64-cmake.patch"

  # no longer including docs, see https://bugs.archlinux.org/task/34185
  # python2 compatibility for generating (html) docs
  #sed -i 's/sphinx-build/sphinx-build2/' cmake/OpenCVDetectPython.cmake
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null

  # SSE only available from Pentium 3 onwards (i686 is way older)
  [[ "$CARCH" = 'i686' ]] && \
    _cmakeopts=('-D ENABLE_SSE=OFF'
      '-DENABLE_SSE2=OFF'
      '-DENABLE_SSE3=OFF')

  # all x64 CPUs support SSE2 but not SSE3
  [[ "$CARCH" = 'x86_64' ]] && _cmakeopts+=('-DENABLE_SSE3=OFF')

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    -DWITH_OPENCL=OFF \
    -DWITH_OPENGL=ON \
    -DWITH_TBB=ON \
    -DWITH_XINE=OFF \
    -DBUILD_WITH_DEBUG_INFO=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DBUILD_EXAMPLES=ON \
    -DINSTALL_C_EXAMPLES=ON \
    -DINSTALL_PYTHON_EXAMPLES=ON \
    -DWITH_GTK=OFF \
    -DWITH_QT=OFF \
    -DWITH_GSTREAMER=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SKIP_RPATH=ON \
    ${_cmakeopts[@]} \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 install

  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/opencv.pc
}
